﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using ANZ_AIM_BLL;
using ANZ_AIM_GUI.Utils;
using log4net;

namespace ANZ_AIM_GUI
{
    public partial class frmShowLog : Form
    {
        //log4net
        private static readonly ILog ilogger = LogManager.GetLogger(typeof(frmShowLog));

        LogBLL logger = new LogBLL();
        public frmShowLog()
        {
            try
            {
                InitializeComponent();
                dtiFrom.Value = dtiTo.Value = DateTime.Now.Date;
                BindData((DataTable)null);
            }
            catch (Exception ex) { throw ex; }
        }

        public void BindData(DataTable dt)
        {
            try
            {
                dgvLog.Rows.Clear();
                dgvLog.ColumnCount = 7;
                dgvLog.Columns[0].Name = "Id";
                dgvLog.Columns[1].Name = "IP Address";
                dgvLog.Columns[2].Name = "User name";
                dgvLog.Columns[3].Name = "Form name";
                dgvLog.Columns[4].Name = "Event name";
                dgvLog.Columns[5].Name = "Created time";
                dgvLog.Columns[6].Name = "Description";

                dt = dt == null ? logger.LogFilter("", dtiFrom.Value.ToShortDateString(), dtiTo.Value.ToShortDateString()) : dt;


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string[] rowData = new string[] { dt.Rows[i][0].ToString(), dt.Rows[i][1].ToString(), dt.Rows[i][2].ToString(), dt.Rows[i][3].ToString(), dt.Rows[i][4].ToString(), dt.Rows[i][5].ToString(), dt.Rows[i][6].ToString() };
                    dgvLog.Rows.Add(rowData);
                }
                foreach (DataGridViewColumn col in dgvLog.Columns)
                {
                    if (col.Name == "Id") { col.Width = 50; }
                    if (col.Name == "IP Address") { col.Width = 100; }
                    if (col.Name == "User name") { col.Width = 100; }
                    if (col.Name == "Form name") { col.Width = 200; }
                    if (col.Name == "Event name") { col.Width = 200; }
                    if (col.Name == "Created time") { col.Width = 150; }
                    if (col.Name == "Description") { col.Width = 350; }
                }
            }
            catch (Exception ex) { throw ex; }
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = logger.LogFilter(txtUsername.Text, dtiFrom.Value.ToString("MM/dd/yyyy"), dtiTo.Value.ToString("MM/dd/yyyy"));
                BindData(dt);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.InnerException == null ? ex.Message : ex.InnerException.Message, Constant.SYS_ERROR, MessageBoxButtons.OK);
                ilogger.Error(ex.Message + " - " + ex.StackTrace);
            }
        }
    }
}
